﻿#include <string.h>
#include <PRESERVECOMMON.HPP>
#include <PRESERVE2.HPP>
#include <CENTRALIZEDMGR.HPP>

CxxlMan::CriticalSec CXXL_DLLEXPORT CxxlMan::IcxxlStorage::csObject;

bool cxxlFASTCALL CxxlMan::cxxlPreserve::Ref(Filter *F)
{
  return true;
}

// 不會有取回的處理
bool cxxlFASTCALL CxxlMan::cxxlPreserve2::Ref(Filter *F)
{
  if(cxxlPreserve::Ref(F) == false)
    return false;

  if(F != NULL)
  {
    CxxlMan::UNICODE_String classid = GetClassID();
    LoadFilter *pLoadFilter = dynamic_cast<LoadFilter*>(F);
    if(pLoadFilter == NULL) // 表示 F 是 SaveFilter
    {
      if(InterfaceID.isNULL())
        return false;
      F->Ref( (wchar_t *)(const wchar_t *)InterfaceID , InterfaceID.StrLength()+1 , INTERFACEIDNAME);

      if(classid.isNULL())
        return false;
      F->Ref( (wchar_t *)(const wchar_t *)classid , classid.StrLength()+1 , CLASSIDNAME);

      CxxlMan::UNICODE_String objid = GetObjID();
      if(objid.isNULL())
        return false;
      F->Ref( (wchar_t *)(const wchar_t *)objid , objid.StrLength()+1 , OBJIDNAME);

      CxxlMan::UNICODE_String group = GetGroup();
      if(group.isNULL() == false)
        F->Ref( (wchar_t *)(const wchar_t *)group , group.StrLength()+1 , GROUPNAME);
      else
        return false;

      ISpirit *Spirit = this->GetSpirit();
      if( Spirit == Spirit_Urgent )
        F->Ref( (wchar_t*)L"Spirit_Urgent", wcslen(L"Spirit_Urgent")+1, SPIRITNAME);
      else
        F->Ref( (wchar_t*)L"Spirit_Easy" , wcslen(L"Spirit_Easy")+1 , SPIRITNAME);
    }
    else // F 是 LoadFilter
    {
      if(pLoadFilter->isChk())
      {
        size_t Count;

        if(pLoadFilter->LoadChk((const wchar_t **)&InterfaceID_bp,&Count,INTERFACEIDNAME) == false)
          return false;

        if(InterfaceID_bp == NULL)
          return false;

        if(pLoadFilter->LoadChk((const wchar_t **)&ClassID_bp,&Count,CLASSIDNAME) == false)
          return false;

        if(classid.isNULL() || wcscmp((const wchar_t*)classid,ClassID_bp) != 0)
          return false;

        if(pLoadFilter->LoadChk((const wchar_t **)&ObjID_bp,&Count,OBJIDNAME) == false)
          return false;

        if(pLoadFilter->LoadChk((const wchar_t **)&Group_bp,&Count,GROUPNAME) == false)
          return false;

        if(pLoadFilter->LoadChk((const wchar_t **)&Spirit_bp,&Count,SPIRITNAME) == false)
          return false;
      }
      else
      {
       // InterfaceID = InterfaceID_bp;

        // 採用自我複製，避開取回的處理
        pLoadFilter->Ref(InterfaceID_bp,0,INTERFACEIDNAME);
        pLoadFilter->Ref(ClassID_bp,0,CLASSIDNAME);
        pLoadFilter->Ref(ObjID_bp,0,OBJIDNAME);
        pLoadFilter->Ref(Group_bp,0,GROUPNAME);
        pLoadFilter->Ref(Spirit_bp,0,SPIRITNAME);
      }
    }
  }
  else
    return false;

  return true;
}


CxxlMan::REGENERARETURN_TYPE cxxlFASTCALL CxxlMan::_Sets::PutPreserve2(const CxxlMan::Smart_Ptr<cxxlPreserve2> &Preserve2_Arg)
{
  return Regenera_NotP2Smart_Ptr;
}

CxxlMan::REGENERARETURN_TYPE cxxlFASTCALL CxxlMan::_Sets::PutEmptyPreserve2()
{
  return Regenera_NotP2Smart_Ptr;
}


class CastToPreserve2:public CxxlMan::ICentralizedMgr
{
public:
  using CxxlMan::ICentralizedMgr::GetPreserve2;
}_CastToPreserve2;


CXXL_DLLEXPORT CxxlMan::cxxlPreserve2 * cxxlFASTCALL CxxlMan::_Preserve2_Cast(const Smart_Ptr<cxxlObject> &Src)
{
  if(Src.isNULL())
    return NULL;

  return (CxxlMan::cxxlPreserve2 *)_CastToPreserve2.GetPreserve2(Src);
}
